package com.work.straighttalk.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/3/3.
 */
public abstract class BaseRVAdapter<T> extends RecyclerView.Adapter<BaseViewHolder<T>> {
    /**
     * @param lists 本adapter需要用到的数据
     */
    private List<T> lists;
    /**
     * @param listener 点击事件监听 由activity或fragment.set
     */
    private OnRecyclerViewClickListener listener;

    public List<T> getLists() {
        return lists;
    }

    public OnRecyclerViewClickListener getListener() {
        return listener;
    }

    public void setListener(OnRecyclerViewClickListener listener) {
        this.listener = listener;
    }

    public void addData(T t) {
        this.lists.add(t);
        notifyItemInserted(getItemCount()-1);
    }

    /**
     * 构造方法初始化数据
     */
    public BaseRVAdapter() {
        this.lists = new ArrayList<>();
    }

    /**
     * 添加数据的方法 外部数据获取后添加进来
     *
     * @param lists
     */
    public void loadData(List<T> lists) {
        this.lists.clear();
        if (null != lists) {
            this.lists.addAll(lists);
        }
        notifyDataSetChanged();
    }

    /**
     * 数据与item绑定
     *
     * @param holder   item对应的holder 继承自BaseViewHolder
     * @param position item 位置
     */
    @Override
    public void onBindViewHolder(BaseViewHolder<T> holder, int position) {
        holder.bindData(lists.get(position));
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }
}
